<?php

namespace app\api\controller;

use think\Controller;
use think\Request;

class Teacher extends controller
{
    public function getTeacherList(Request $request)
    {
        $type = $request->get('type', 1, 'intval');
        $age = $request->get('age', '', 'trim');
        $sex = $request->get('sex', 0, 'intval');
        $teacher = Db('teacher');
        if (preg_match('/^\d+,\d+$/',$age)) {
            $teacher->where('age', 'BETWEEN', $age);
        }
        if ($sex) {
            $teacher->where('sex', $sex);
        }
        $list = $teacher->where('type', $type)
            ->field('teacher_id,name,avatar,age,sex,tags,type,description')
            ->order('sort asc')
            ->select();
        return ['code' => 0, 'data' => $list];
    }

    public function getTeacherInfo(Request $request)
    {
        $teacher_id = $request->get('teacher_id', 0, 'intval');
        $info = Db('teacher')
            ->where('teacher_id', $teacher_id)
            ->find();
        if (!$info) {
            return ['code' => 2, 'msg' => '讲师已删除'];
        }
        $info['article'] = Db('article')->where('teacher_id', $teacher_id)->select();
        return ['code' => 0, 'data' => $info];
    }
}
